import ResManager from "../core/ResManager";
import Unit from "./Unit";
import Animator from "./view/Animator";

export default class AnimationComponent {
    unit: Unit = null;
    direction: cc.Vec2 = null;
    animClips: Map<string,cc.AnimationClip> = new Map<string,cc.AnimationClip>();
    animation:cc.Animation = null;
    animator:Animator = null;
    currentState:string = null;
    constructor(unit:Unit,animData?) {
        this.unit = unit;
        unit.anim = this;
        this.animator =  unit.node.getComponent(Animator);
        if (!this.animator) {
            this.animation = unit.node.getComponent(cc.Animation);
        }
        if (animData) {
            for (const key in animData) {
                const element = animData[key];
                let clip = ResManager.instance.getRes(element,cc.AnimationClip) as cc.AnimationClip;
                this.animClips.set(key,clip);
            }
        }
    }
    switchDirection(dir: cc.Vec2) {
        this.direction = dir;
        this.animation.play(this.currentState+this.direction.x + this.direction.y);
    }
    clear(){
        if (this.animator) {
            return;
        }
        this.animation.stop();
    }
    play(animName:string) {
        
        this.currentState = animName;
        if (this.animator) {
            this.animator.play(animName);
            return;
        }
        //this.animation.play(this.currentState+this.direction.x + this.direction.y);
    }
}